package com.activity.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.activity.bean.CategoryBean;
import com.donkingliang.groupedadapter.adapter.GroupedRecyclerViewAdapter;
import com.donkingliang.groupedadapter.holder.BaseViewHolder;
import com.example.apple.ddhr_new.R;
import com.example.apple.ddhr_new.adapter.BaseAdapter;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class CategoryAdapter extends BaseAdapter<CategoryBean> {
    public OnLongClickListener longClickListener;
    public boolean isEdit;

    public CategoryAdapter(Context context) {
        super(context);
    }

    @Override
    public BaseViewHolder<CategoryBean> getViewHolder(int viewType, View view) {
        return new CategoryHolderView(view);
    }

    @Override
    public boolean hasHeader(int groupPosition) {
        return true;
    }

    @Override
    public int getGroupCount() {
        return getData().size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if(isEdit)
            return 0;
        return   getData().get(groupPosition).getSublist().size();

    }

    @Override
    public int getHeaderLayout(int viewType) {
         return R.layout.header_category;
    }

    @Override
    public int getChildLayout(int viewType) {
        return R.layout.item_category;
    }

    public class CategoryHolderView extends BaseViewHolder<CategoryBean> {

        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_delete)
        TextView tvDelete;
        public CategoryHolderView(@NonNull View itemView) {
            super(itemView);
        }

        @Override
        public void setData(CategoryBean data) {
            tvName.setText(data.getWorkscategoryname());

            if(isEdit){
                tvDelete.setVisibility(View.VISIBLE);
            }else {
                tvDelete.setVisibility(View.GONE);
            }

        }
    }

    @Override
    public void onBindChildViewHolder(com.donkingliang.groupedadapter.holder.BaseViewHolder holder, int groupPosition, int childPosition) {
        ((CategoryHolderView)holder).setData(getData().get(groupPosition).getSublist().get(childPosition));
//        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
//            @Override
//            public boolean onLongClick(View v) {
//
//
//                if(longClickListener!=null){
//                    longClickListener.OnLongClickListener(CategoryAdapter.this,holder,groupPosition,childPosition);
//                }
//                return true;
//            }
//        });
        ((CategoryHolderView)holder).tvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(longClickListener!=null){
                   longClickListener.OnLongClickListener(CategoryAdapter.this,holder,groupPosition,childPosition);
                }
            }
        });
    }

    @Override
    public void onBindHeaderViewHolder(com.donkingliang.groupedadapter.holder.BaseViewHolder holder, int groupPosition) {
        super.onBindHeaderViewHolder(holder, groupPosition);
        ((CategoryHolderView)holder).setData(getData().get(groupPosition));
        ((CategoryHolderView)holder).tvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(longClickListener!=null){
                    longClickListener.OnLongHeaderClickListener(CategoryAdapter.this,holder,groupPosition);
                }
            }
        });
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


        if(viewType==TYPE_CHILD){
            View view = LayoutInflater.from(mContext).inflate(getChildLayout(viewType), parent, false);
            RecyclerView.ViewHolder holder = getViewHolder(viewType, view);

            return  holder;
        }else if(viewType==TYPE_HEADER)
        {
            View view = LayoutInflater.from(mContext).inflate(getHeaderLayout(viewType), parent, false);
            RecyclerView.ViewHolder holder = getViewHolder(viewType, view);

            return  holder;
        }

        return super.onCreateViewHolder(parent,viewType);
    }

    public  interface OnLongClickListener{
        void  OnLongHeaderClickListener (GroupedRecyclerViewAdapter adapter, com.donkingliang.groupedadapter.holder.BaseViewHolder baseViewHolder, int groupPosition);
        void  OnLongClickListener (GroupedRecyclerViewAdapter adapter, com.donkingliang.groupedadapter.holder.BaseViewHolder baseViewHolder, int groupPosition, int childPosition);
    }
}
